import { Card, Table } from 'antd'
import { ColumnsType } from 'antd/lib/table'
import { RightOutlined, DownOutlined } from '@ant-design/icons'
import React, { useEffect, useState } from 'react'
import { getDictList } from '@api/hospital/hospitalList'
import { IDictList } from '@api/hospital/model/hospitalListTypes'

export default function Dict() {
    // 声明状态
    let [dictList, setDictList] = useState<IDictList>([])
    const columns: ColumnsType<any> = [
        {
            title: '名称',
            dataIndex: 'name'
        },
        {
            title: '编码',
            dataIndex: 'dictCode'
        },
        {
            title: '值',
            dataIndex: 'value'
        },
        {
            title: '创建时间',
            dataIndex: 'createTime'
        }
    ]
    async function _getDictList() {
        let res = await getDictList("1");
        // console.log(res);
        setDictList(res);
    }
    useEffect(() => {
        _getDictList()
    }, [])
    return (
        <Card>
            <Table
                rowKey={'id'}
                columns={columns}
                dataSource={dictList}
                pagination={false}
                expandable={{
                    expandIcon: ({ expanded, onExpand, record }) =>{
                        //console.log('expanded: ', expanded); // 展开 收起 标识 boolean
                        //console.log('onExpand: ', onExpand);// 展开节点的函数
                        // console.log('record: ', record); // 当前行数据对象

                        if(!record.hasChildren){// 该节点没有子节点
                            return <div style={{display:'inline-block',width:15}}></div>
                        }
                        // 当record 有子节点，将子节点放入 record对象的 children属性上，在使用onExpand函数即可展开
                        // 自己点的数据，是根据父节点的value 向数据库请求获取的
                        return expanded ? (
                            <DownOutlined onClick={e => onExpand(record, e)} />
                        ) : (
                            <RightOutlined onClick={async e => {
                                if(!record.children?.length ){ // record没有children属性或者 children属性数组的长度是0

                                    // 发送ajax请求获取子节点数据
                                    let childList = await getDictList(record.value)
                                    console.log('childList: ', childList);
                                    record.children = childList;
                                }
                                return onExpand(record, e)
                            }} />
                        )
                    }
                }}
            />
        </Card>
    )
}
